import chardet
import argparse
import codecs

def Get_P_and_E():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--path', default=-1, help="path")
    parser.add_argument('-e', '--encodings', default=-1, help='encoding')
    args = parser.parse_args()
    path = args.path
    encodings = args.encodings
    return path, encodings

def case1(path,encodings):
    with codecs.open(filename=path, mode="r", encoding=encodings) as f:
        data = f.read()
        with codecs.open(filename=path, mode="w", encoding='utf-8') as f1:
            f1.write(data)
            f1.close()
        f.close()
        print("已成功转存为utf-8格式")

def case2(path):
    f= open(path, 'rb')
    print(f"您未输入编码格式，系统猜出您的编码格式为{chardet.detect(f.read())['encoding']}")
    f.close()

if __name__ == '__main__':
    path, encodings = Get_P_and_E()
    if encodings != -1:
        case1(path,encodings)
    else:
        case2(path)